package com.cn.http.http;


import com.cn.http.enums.TypeBodyEnum;
import com.cn.http.http.impl.FileUpload;
import com.cn.http.utils.MapUtils;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

/**
 * &#064;Time 2024 八月 星期五 16:30
 *
 * @author ShangGuan
 */
public interface HttpParam {

    void setType(TypeBodyEnum type);

    TypeBodyEnum getType();

    void setBody(String body);

    void setUrlForm(String urlForm);

    void setFileUpload(FileUpload fileUpload);

    FileUpload getFileUpload();
    /**
     * 获取参数json字符
     *
     * @return {@link String }
     */
    String getBody();

    String getUrlForm() ;

    default void setForm(String key, String value) throws UnsupportedEncodingException {
        Map<String, Object> objectObjectHashMap = new HashMap<>();
        objectObjectHashMap.put(key, value);
        setForm(objectObjectHashMap);
    }

    default void setForm(Map<String, Object> param) throws UnsupportedEncodingException {
        if (!MapUtils.isEmpty(new HashMap<>(param))) {
            setUrlForm(MapUtils.mapToUrlParams(param));
            return;
        }
        throw new RuntimeException("params is empty");
    }


    default void setBody(String key, String value) {
        Map<String, Object> objectObjectHashMap = new HashMap<>();
        objectObjectHashMap.put(key, value);
        setBody(objectObjectHashMap);
    }

    default void setBody(Map<String, Object> param) {
        //如果没有设置类型 则默认不添加任何body参数
        TypeBodyEnum type = getType();
        if(type!=null){
            if (!MapUtils.isEmpty(new HashMap<>(param))) {
                setBody(type.to(param));
                return;
            }
            throw new RuntimeException("params is empty");
        }

    }


}
